package com.ymttest.database.mongo;


import java.util.List;

import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationOptions;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import com.ymttest.database.model.mongo.query.Order;
import com.ymttest.database.mongo.config.MongoOrderConfig;
import com.ymttest.database.mongo.config.OrderField;

import static com.ymttest.database.mongo.config.SchemaField.ORDER;


public class OrderQueryWapper {
	

    private MongoTemplate mongoOrderTemplate;
    
    public OrderQueryWapper(){
    	try {
			mongoOrderTemplate = new MongoOrderConfig().mongoTemplate();
		} catch (Exception e) {
			e.printStackTrace();
		}
    }

    /**
     * 根据订单号获取订单详细信息
     *
     * @param orderId
     * @return
     */
    public Order findById(long orderId) {
        return mongoOrderTemplate.findById(orderId, Order.class, ORDER);
    }

    public List<Order> findByIds(List<Long> orderIdList){
        return mongoOrderTemplate.find(Query.query(Criteria.where(OrderField.ID).in(orderIdList)), Order.class, ORDER);
    }

    public List<Order> queryByCriteria(Criteria criteria) {
        return mongoOrderTemplate.find(Query.query(criteria), Order.class, ORDER);
    }

    public List<Order> find(Query query) {
        return mongoOrderTemplate.find(query, Order.class, ORDER);
    }

    public Order findOneByCriteria(Criteria criteria) {
        return mongoOrderTemplate.findOne(Query.query(criteria), Order.class, ORDER);
    }

    public Order findOneByQuery(Query query) {
        return mongoOrderTemplate.findOne(query, Order.class, ORDER);
    }

    public <T> List<T> aggregation(Aggregation aggregation, Class<T> outputType){
        aggregation = aggregation.withOptions(new AggregationOptions.Builder().allowDiskUse(true).build());
        return mongoOrderTemplate.aggregate(aggregation, ORDER, outputType).getMappedResults();
    }
    
    public static void main(String[] args) {
		Order order = new OrderQueryWapper().findById(112462970);
		System.out.println(order.getAddress());
		System.out.println(order.getTimeMongoAdd());
	}
}
